/**
 * 
 */
package org.u2ml.stack.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;

import com.noelios.restlet.ext.servlet.ServerServlet;

/**
 * Extended ServerServlet.
 * 
 * may threadless delai response :
 * 
 * If request parameter contains "wait", response is postponed, client will wait.
 * If request parameter contains "awake", each waiting clients will be answer.
 * 
 * 
 * @author Pascal
 *
 */
@SuppressWarnings("serial")
public class PushServlet extends ServerServlet {
	
	private static final long INDEFINITLY = 0;

	/**
	 * run or delai herited ServerServlet service method 
	 *  
	 * @see com.noelios.restlet.ext.servlet.ServerServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
		WaitingClients waitingClients = WaitingClients.getInstance();
		if (request.getParameter("SESSIONID") != null && request.getParameter("wait") != null)
		{
			Continuation continuation = ContinuationSupport.getContinuation(request, new Object());
			waitingClients.add(request.getParameter("SESSIONID"), new Client(continuation));
			continuation.suspend(INDEFINITLY);
			
		}		
		super.service(request, response);
		
	}

}
